package org.ecloud.secret.provider;

import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.engine.PageQuery;
import org.ecloud.biz.secret.model.SecretPo;
import org.ecloud.biz.secret.service.SecretQueryService;
import org.ecloud.common.constants.Status;
import org.ecloud.core.entity.APIResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "密钥管理", value = "密钥获取")
@RestController
@RequestMapping("/")
public class SecretProvider {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private SecretQueryService secretQueryService;
	
	@ApiOperation(value = "获取密钥列表", notes = "无参获取所有密钥数据")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public APIResult<List<SecretPo>> list(){
		APIResult<List<SecretPo>> result = new APIResult<>(Status.SUCCESS.code());
		try{
			List<SecretPo> datas = secretQueryService.findAll();
			result.setData(datas);
		}catch(Exception e){
			result.setCode(Status.FAILURE_SECRET_FINDALL.code());
			result.setMessage(Status.FAILURE_SECRET_FINDALL.text());
			result.setCause(e.getMessage());
			logger.error(Status.FAILURE_SECRET_FINDALL.text(), e);
		}
		
		return result;
	}
	
	@ApiOperation(value = "分页获取密钥列表", notes = "分页获取密钥数据")
	@RequestMapping(value = "list/page", method = RequestMethod.GET)
	public APIResult<PageQuery<SecretPo>> listPage(
			@ApiParam(name = "page", value = "页码", required = true) @RequestParam(name = "page", required = true) long page, 
			@ApiParam(name = "limit", value = "页容量", required = true) @RequestParam(name = "limit", required = true) long limit){
		APIResult<PageQuery<SecretPo>> result = new APIResult<>(Status.SUCCESS.code());
		try{
			PageQuery<SecretPo> pageQuery = new PageQuery<SecretPo>(page, limit);
			secretQueryService.query(pageQuery);
			result.setData(pageQuery);
		}catch(Exception e){
			result.setCode(Status.FAILURE_SECRET_QUERY.code());
			result.setMessage(Status.FAILURE_SECRET_QUERY.text());
			result.setCause(e.getMessage());
			logger.error(Status.FAILURE_SECRET_QUERY.text(), e);
		}
		
		return result;
	}
	
}
